package com.streamax.app.cqbus.platform.socket.tcp.command

import com.streamax.app.cqbus.platform.ext.capture
import com.streamax.app.cqbus.platform.ext.toInt2
import com.streamax.app.cqbus.platform.ext.toInt4

data class TcpResponse83(
        /**
         * 应答结果，请求结果，0：成功，1：失败
         */
        var result: Byte = 0,

        var mToken: Int = 0,
        /**
         *  总包数
         */
        var packetCount: Int = 0,
        /**
         * 当前包序号，从1开始
         */
        var currentSn: Int = 1,
        /**
         * 包大小
         */
        var packetSize: Int = 0,
        /**
         *
         */
        var resultBuffer: ByteArray = byteArrayOf(),

        ) {

    fun decode(buffer: ByteArray) : TcpResponse83 {
        var offset = 0
        result = buffer[offset]
        offset += 1
        if (result.toInt() == 0x00) {//成功
            mToken = buffer.capture(offset, offset + 3).toInt4
            offset += 4
            packetCount = buffer.capture(offset, offset + 1).toInt2
            offset += 2
            currentSn = buffer.capture(offset, offset + 1).toInt2
            offset += 2
            packetSize = buffer.capture(offset, offset + 1).toInt2
            offset += 2
            resultBuffer = buffer.capture(offset, buffer.size  -1)
        }
        return this
    }
}